


import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/model/mine/model/finance_stat_m.dart';
import 'package:bike_ride/model/pay/model/overseas_pay_m.dart';
import 'package:bike_ride/model/wallet/model/open_pay_create_order_m.dart';
import 'package:bike_ride/model/wallet/model/recharge_info_m.dart';
import 'package:bike_ride/model/wallet/model/refund_info_m.dart';
import 'package:bike_ride/model/wallet/view/wallet_v.dart';
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class WalletViewModel extends PageViewModel<WalletViewState> {
  CancelToken? cancelToken;

  DiscountStatM? discountStatM;
  RefundInfoM? refundInfoM;
  RechargeInfoM? rechargeInfoM;
  OpenPayCreateOrderM? openPayCreateOrderM;
  OverseasPayM? overseasPayM;
  FiuuPayM? fiuuPayM;

  bool startTimer = false;
  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    getFinanceInfo();
    getRefundInfo();
    getRechargeInfo(needShowRechargeDialog: state.widget.arguments?['show_charge_dialog'] == true);
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }


  Future getFinanceInfo() async {
    final result = await CommonRepository.getFinanceInfoIdl(cancelToken: cancelToken);
    discountStatM = result;
    state.notifyChange();
  }

  Future getRefundInfo() async {
    final result = await CommonRepository.getRefundInfoIdl(cancelToken: cancelToken);
    refundInfoM = result;
    state.notifyChange();
  }

  Future getRechargeInfo({bool needShowRechargeDialog = false}) async {
    final result = await CommonRepository.getRechargeInfoIdl(cancelToken: cancelToken);
    rechargeInfoM = result;
    if(result != null){
      if(needShowRechargeDialog){
        state.showRechargeDialog();
      }
    }
    state.notifyChange();
  }

  bool isGetOverseasPay = false;
  Future getOverseasOpenPay(num? totalAmount) async {
    if(isGetOverseasPay)return;
    isGetOverseasPay = true;
    final result = await CommonRepository.getOverseasOpenPayIdl(
        cancelToken: cancelToken, totalAmount: totalAmount,actionType: 1);
    overseasPayM = result;
    // if(result?.qrCode != null && result!.qrCode != ''){
    //   //显示二维码
    //   state.showPayErCodeDialog();
    // }
    if(result?.secureToken != null && result!.secureToken != ''){
      state.navigateToPayView(result.secureToken!);
    }
    isGetOverseasPay = false;
  }

  bool isGetOverseasH5Pay = false;
  Future getOverseasH5Pay(num? totalAmount,{required num payChanel}) async {
    if(isGetOverseasH5Pay)return;
    isGetOverseasH5Pay = true;
    final result = await CommonRepository.getOverseasH5PayIdl(
        cancelToken: cancelToken, totalAmount: totalAmount,actionType: 1,payChanel: payChanel);
    fiuuPayM = result;
    if(result?.payApi != null && result!.payApi != ''){
      state.navigateToPayView(result.payApi!);
    }
    isGetOverseasH5Pay = false;
  }


  bool isGetOverseasPayStatus = false;
  Future<bool> getOverseasPayStatus() async {
    if(isGetOverseasPayStatus)return false;
    try{
      isGetOverseasPayStatus = true;
      String? outTradeNo = overseasPayM?.outTradeNumber ?? fiuuPayM?.outTradeNo;
      if(outTradeNo != null && outTradeNo != ''){
        final result = await CommonRepository.getOverseasPayStatusIdl(
            cancelToken: cancelToken, outTradeNo: outTradeNo);
        if(result){
          state.dismissErCodeDialog();
          EasyLoading.showSuccess(S.current.rechargeSuccess);
          getFinanceInfo();
        }
        return result;
      }
    }finally{
      isGetOverseasPayStatus = false;
    }
    return false;
  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}